# -*- coding: utf-8 -*-
# 示例来源：https://www.runoob.com/pandas/pandas-json.html
import json
import pandas as pd

URL = 'https://static.runoob.com/download/sites.json'
df = pd.read_json(URL)
print(df)

# 使用到 json_normalize() 方法将内嵌的数据完整的解析出来：
URL = '../data/point.json'
with open(URL,'r') as f:
    data = json.loads(f.read())
print(data)

# 正则化json的features要素集中的单个要素，将数据格式转为dataframe
df = pd.json_normalize(data,record_path=['features'])
print(df['geometry.coordinates'])

# 一列变两列，拆分经纬度坐标字段为两个字段
df[['WGS84_lng', 'WGS84_lat']] = df['geometry.coordinates'].astype('string').str.split(',', 1, expand = True)
# 将[替换掉
df['WGS84_lng']=df['WGS84_lng'].str.replace('[','', regex=True)
# 将]替换掉
df['WGS84_lat']=df['WGS84_lat'].str.replace(']','', regex=True)

# 删除原来的geometry.coordinates字段
df.drop('geometry.coordinates',axis=1,inplace=True)

print(df)
# 输出修改后的dataframe
df.to_csv('../data/point.csv')